package com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter;

import android.content.Context;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.AddressItemBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.CommonAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.config.ViewHolder;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.NewAddressActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.checkbox.SmoothCheckBox;

import java.util.List;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/9.
 * Desc   :
 */

public class AddressAdapter extends CommonAdapter<AddressItemBean> {

    private OnClickListener listener;

    public AddressAdapter(Context context, int layoutId, List<AddressItemBean> datas) {
        super(context, layoutId, datas);
    }

    @Override
    protected void convert(ViewHolder holder, AddressItemBean bean, int position) {
        TextView name = holder.getView(R.id.tv_address_name);
        TextView phone = holder.getView(R.id.tv_address_phone);
        TextView address = holder.getView(R.id.tv_address_detail);
        SmoothCheckBox checkBox = holder.getView(R.id.cb_default_address);
        TextView tvEdit = holder.getView(R.id.tv_edit_address);
        TextView tvDelete = holder.getView(R.id.tv_delete_address);
        name.setText(bean.getShop_name());
        phone.setText(bean.getPhone());
        address.setText(bean.getProvince() + bean.getCity() + bean.getArea() + bean.getAddress());
        tvEdit.setOnClickListener(v -> mContext.startActivity(NewAddressActivity.createIntent(mContext, "edit", bean)));
        tvDelete.setOnClickListener(v -> listener.onDeleteClick(bean.getId()));
        checkBox.setChecked(bean.getDefaults().equals("1"));
        checkBox.setOnToggleClickListener(isChecked -> listener.onDefaultClick(bean.getId(), (isChecked ? "1" : "0")));
        holder.itemView.setOnClickListener(v -> mOnItemClickListener.onItemClick(this, position));
    }

    public interface OnClickListener {

        void onDefaultClick(String address_id, String isDefault);

        void onDeleteClick(String address_id);

    }

    public void setOnDeleteClickListenr(OnClickListener listener) {
        this.listener = listener;
    }
}
